package com.ruoyi.system.service.impl;

import com.ruoyi.common.bo.FileRefBO;
import com.ruoyi.common.core.domain.vo.FileRefGrantVO;
import com.ruoyi.common.core.query.FileRefListQuery;
import com.ruoyi.system.mapper.FileRefMapper;
import com.ruoyi.system.service.FileRefService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
@Slf4j
public class FileRefServiceImpl implements FileRefService {

    private final FileRefMapper fileRefMapper;

    /**
     * {
     *   "page": 1,
     *   "pageSize": 10,
     *   "type": 0, // 用户
     *   "permission": 0, // 新建文件夹
     *   "search": "张", // 模糊搜索关键字
     *   "isAssigned": false // 未授权
     * }
     *
     * {
     *   "list": [
     *     {
     *       "id": 1,
     *       "name": "张三"
     *     },
     *     {
     *       "id": 3,
     *       "name": "张伟"
     *     }
     *   ],
     *   "total": 15, // 总共有 15 个未授权的用户
     *   "page": 1,
     *   "pageSize": 10
     * }
     */

    @Override
    public Map list(FileRefListQuery query) {
        Map<String,Object> resultMap = new HashMap<>();
        List<FileRefBO> unassignedData = new ArrayList<>();
        List<Long> assignedList = new ArrayList<>();
        //通过isAssigned判断查询的是已分配还是未分配还是全部都查
        if (query.getIsAssigned() == 3) {//查全部
            query.setIsAssigned(1);
            int offset = (query.getAssignPageNum()-1) * query.getAssignPageSize();
            query.setOffset(offset);
            List<FileRefBO> fileRefBOS1 = fileRefMapper.selectPermissionList(query);

            query.setIsAssigned(0);
            offset = (query.getUnassignPageNum()-1) * query.getUnassignPageSize();
            query.setOffset(offset);
            List<FileRefBO> fileRefBOS2 = fileRefMapper.selectPermissionList(query);

            //组装数据
            unassignedData.addAll(fileRefBOS1);
            unassignedData.addAll(fileRefBOS2);

            assignedList = fileRefBOS1.stream().map(FileRefBO::getId).collect(Collectors.toList());

        }else{
            if (query.getIsAssigned() == 1) {//查已分配
                int offset = (query.getAssignPageNum()-1) * query.getAssignPageSize();
                query.setOffset(offset);
            }else {
                int offset = (query.getUnassignPageNum()-1) * query.getUnassignPageSize();
                query.setOffset(offset);
            }
            List<FileRefBO> fileRefBOs = fileRefMapper.selectPermissionList(query);

            if (query.getIsAssigned() == 1) {//查已分配
                assignedList = fileRefBOs.stream().map(FileRefBO::getId).collect(Collectors.toList());
            }
            unassignedData.addAll(fileRefBOs);
        }
        resultMap.put("unassignedData",unassignedData);
        resultMap.put("assignedList",assignedList);
        resultMap.put("code",200);
        resultMap.put("msg","查询成功");
        return resultMap;
    }

    /**
     * 授权
     * @param vo
     * @return
     */
    @Override
    public String grant(FileRefGrantVO vo) {
        //todo 判断用户权限

        //
        return null;
    }


}
